package com.heu.blood.autologous.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heu.blood.application.entity.BloodApplicationItemEntity;
import com.heu.blood.autologous.dao.BloodInventoryAutologousDao;
import com.heu.blood.autologous.dictenum.ApplicationStateAutologousEnum;
import com.heu.blood.autologous.dictenum.BloodStateAutologousEnum;
import com.heu.blood.autologous.dictenum.InventoryStateAutologousEnum;
import com.heu.blood.autologous.entity.*;
import com.heu.blood.autologous.exception.SendAutologousExceptionEnum;
import com.heu.blood.autologous.service.*;
import com.heu.blood.autologous.vo.BloodSendAutologousVo;
import com.heu.blood.common.Constant.ConstantData;
import com.heu.blood.common.utils.GenerateNumberBySnowFlake;
import com.heu.blood.globalExceptionHandler.MyException;
import com.heu.blood.match.dictenum.BloodStateEnum;
import com.heu.blood.match.entity.BloodInformationEntity;
import com.heu.blood.storge.dictenum.OutStorageReasonEnum;
import com.heu.blood.storge.entity.BloodOutStorgeEntity;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;

import com.heu.blood.autologous.dao.BloodSendAutologousDao;
import org.springframework.transaction.annotation.Transactional;


@Service("bloodSendAutologousService")
public class BloodSendAutologousServiceImpl extends ServiceImpl<BloodSendAutologousDao, BloodSendAutologousEntity> implements BloodSendAutologousService {

    @Autowired
    private BloodSendAutologousDao bloodSendAutologousDao;
    @Autowired
    private  BloodSendAutologousService bloodSendAutologousService;
    @Autowired
    private BloodInformationAutologousService bloodInformationAutologousService;
    @Autowired
    private BloodApplicationAutologousService bloodApplicationAutologousService;
    @Autowired
    private BloodOutStorgeAutologousService bloodOutStorgeAutologousService;
    @Autowired
    private BloodInventoryAutologousService bloodInventoryAutologousService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BloodSendAutologousEntity> page = this.page(
                new Query<BloodSendAutologousEntity>().getPage(params),
                new QueryWrapper<BloodSendAutologousEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public int sendBloodAutologous(BloodSendAutologousEntity bloodSendAutologous) {

        //若重复发血则抛异常
        LambdaUpdateWrapper<BloodSendAutologousEntity> bloodSendAutologousWrapper = new LambdaUpdateWrapper<>();
        bloodSendAutologousWrapper.eq(BloodSendAutologousEntity::getBloodId,bloodSendAutologous.getBloodId());
        int count = bloodSendAutologousDao.selectCount(bloodSendAutologousWrapper);

        if (count > 0){
            throw new MyException(SendAutologousExceptionEnum.DUPLICATE_BLOOD_SEND);
        }

        bloodSendAutologous.setOperatorTime(new Date());

        int insert = bloodSendAutologousDao.insert(bloodSendAutologous);

        LambdaUpdateWrapper<BloodInformationAutologousEntity> bloodInfoUpdateWrapper = new LambdaUpdateWrapper<>();
        bloodInfoUpdateWrapper.set(BloodInformationAutologousEntity::getBloodState, BloodStateAutologousEnum.AUTO_BLOOD_ALREADY_SEND.code());
        bloodInfoUpdateWrapper.eq(BloodInformationAutologousEntity::getBloodId, bloodSendAutologous.getBloodId());
        boolean updateInformation = bloodInformationAutologousService.update(bloodInfoUpdateWrapper);

        LambdaUpdateWrapper<BloodApplicationAutologousEntity> bloodApplicationUpdateWrapper = new LambdaUpdateWrapper<>();
        bloodApplicationUpdateWrapper.set(BloodApplicationAutologousEntity::getApplicationState, ApplicationStateAutologousEnum.BLOOD_SEND.code());
        bloodApplicationUpdateWrapper.eq(BloodApplicationAutologousEntity::getApplicationId,bloodSendAutologous.getApplicationId());
        boolean updateApplication = bloodApplicationAutologousService.update(bloodApplicationUpdateWrapper);

        if (insert==1&&updateInformation&&updateApplication){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public List<BloodSendAutologousVo> selectSendBloodWithId(BloodSendAutologousEntity bloodSendAutologous) {
        return bloodSendAutologousDao.selectSendBloodWithId(bloodSendAutologous);
    }

    @Override
    public int sureGetAutologousBlood(BloodSendAutologousVo bloodSendAutologousVo) {
        // 创建一个出库的单据号
        GenerateNumberBySnowFlake generateNumberBySnowFlake = new GenerateNumberBySnowFlake(ConstantData.AUTOLOGOUS_BLOOD_OUT_STORAGE_NO_WORK_ID, ConstantData.AUTOLOGOUS_BLOOD_OUT_STORAGE_NO_PREFIX);
        String outStorageId = generateNumberBySnowFlake.genNumberByflag();

        // 1.设置完成时间之后，取血人以及取血时间的更新

        LambdaUpdateWrapper<BloodSendAutologousEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(BloodSendAutologousEntity::getBloodId,bloodSendAutologousVo.getBloodId());
        lambdaUpdateWrapper.set(BloodSendAutologousEntity::getRecipientTime,new Date());
        lambdaUpdateWrapper.set(BloodSendAutologousEntity::getRecipientId,bloodSendAutologousVo.getRecipientId());
        lambdaUpdateWrapper.set(BloodSendAutologousEntity::getRecipientName,bloodSendAutologousVo.getRecipientName());
        bloodSendAutologousService.update(lambdaUpdateWrapper);



        // 2.修改血液状态为已使用，只要是使用了就不可以退回去了
        LambdaUpdateWrapper<BloodInformationAutologousEntity> bloodInfoUpdateWrapper = new LambdaUpdateWrapper<>();
        bloodInfoUpdateWrapper.set(BloodInformationAutologousEntity::getBloodState,BloodStateAutologousEnum.AUTO_USED.code());
        bloodInfoUpdateWrapper.set(BloodInformationAutologousEntity::getBloodBankDeliveryId,outStorageId); // 设置出库编号
        bloodInfoUpdateWrapper.eq(BloodInformationAutologousEntity::getBloodId,bloodSendAutologousVo.getBloodId());
        bloodInformationAutologousService.update(bloodInfoUpdateWrapper);

        // 3.在出库表里面塞一份
        BloodOutStorgeAutologousEntity outStorgeEntity = new BloodOutStorgeAutologousEntity();
        BeanUtils.copyProperties(bloodSendAutologousVo,outStorgeEntity); // 拷贝一份数据进来
        outStorgeEntity.setBloodTaker(bloodSendAutologousVo.getRecipientName());
        outStorgeEntity.setBloodTakerNumber(bloodSendAutologousVo.getRecipientId());
        outStorgeEntity.setOutStorgeReason(OutStorageReasonEnum.TRANSFUSION.code());// 出库原因设为输血
        outStorgeEntity.setOutStorgeNumber(outStorageId);// 设置出库单据号
        outStorgeEntity.setOutStorgeTime(bloodSendAutologousVo.getRecipientTime());
        outStorgeEntity.setOutStorgeLocation(bloodSendAutologousVo.getBloodLocation());
        outStorgeEntity.setOperator(bloodSendAutologousVo.getOperatorName());
        bloodOutStorgeAutologousService.save(outStorgeEntity);

        // 4.修改签收库存表状态
        String bloodId = bloodSendAutologousVo.getBloodId();
        LambdaUpdateWrapper<BloodInventoryAutologousEntity> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(BloodInventoryAutologousEntity::getBloodId, bloodId);
        boolean exists = bloodInventoryAutologousService.count(queryWrapper) > 0;

// 如果存在才更新状态
        if (exists) {
            LambdaUpdateWrapper<BloodInventoryAutologousEntity> lambdaUpdateWrapper1 = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper1.set(BloodInventoryAutologousEntity::getTransferStatus, InventoryStateAutologousEnum.AUTO_DISPATCHED.code());
            lambdaUpdateWrapper1.eq(BloodInventoryAutologousEntity::getBloodId, bloodId);
            bloodInventoryAutologousService.update(lambdaUpdateWrapper1);
        }


        return 0;
    }

}